package com.rrd.dw.mr.gzip.kafka;

import java.io.IOException;
import java.util.Date;
import java.util.Set;
import java.util.regex.Pattern;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import com.amazonaws.util.Md5Utils;
import com.rrd.dw.utils.DateUtil;
import com.rrd.dw.utils.MD5Util;
import com.ucredit.babel.common.kafka.CrawlResult;
import com.ucredit.babel.common.model.Job;
import com.ucredit.babel.common.model.behavior.AppInfo;
import com.ucredit.babel.common.model.behavior.DeviceInfo;
import com.ucredit.babel.common.model.behavior.EventInfo;
import com.ucredit.babel.common.model.behavior.UserBehaviorRecord;
import com.ucredit.babel.common.model.behavior.UserInfo;
import com.ucredit.babel.common.model.unionpaysmart.Personal;
import com.ucredit.babel.common.model.unionpaysmart.UnionPaySmartPersonal;

/**
 * 解析银联智慧数据
 * @author liubaoxin
 */
@Deprecated
public class UnionPaySmartParse {
	public static final String CRT001 =  "\001";
	public static final Pattern CRT001_PATTERN =  Pattern.compile("\001");
	private  String id="";
	private String timestamstr="";
	private String job_id ="";
	private String id_no="";
	private StringBuilder sb = new StringBuilder(500);
	private  Text val = new Text();
	private  Text key= new Text();
	/**
	 * 银联智慧数据
	 * @param userBehavior
	 * @throws InterruptedException 
	 * @throws IOException 
	 */
	public  void parseUnionPaySmart(Context context,UnionPaySmartPersonal unionPaySmart,String successful) throws IOException, InterruptedException{
    	Job job = unionPaySmart.getJob();
    	Personal personal = unionPaySmart.getPersonal();
    	CrawlResult result=job.getResult();
    	sb.delete(0,sb.length());
    	//数据时间
    	this.timestamstr=unionPaySmart.getTimestamp()==null?"":DateUtil.formateDateStr(new Date(unionPaySmart.getTimestamp()));
    	//身份证号
    	this.id_no =unionPaySmart.getIdNumber()==null?"":unionPaySmart.getIdNumber();
    	//job_id
    	this.job_id = job.getJobID()==null?"":job.getJobID();
    	this.id = MD5Util.getMd5(this.id_no+this.timestamstr+this.job_id);
 
    	sb.append(this.id).append(CRT001).append(this.timestamstr).append(CRT001).append(this.id_no).append(CRT001);
    	//响应http状态
    	sb.append(personal.getCode()==null?"":personal.getCode()).append(CRT001);
    	//业务状态  2000是成功
    	sb.append(personal.getStatus()==null?"":personal.getStatus()).append(CRT001);
    	//返回提示信息
    	sb.append(personal.getMessage()==null?"":personal.getMessage()).append(CRT001);
    	//流水号
    	sb.append(personal.getSmartId()==null?"":personal.getSmartId()).append(CRT001);
    	//数据
    	sb.append(personal.getData()==null?"":personal.getData()).append(CRT001);
    	//jobid
    	sb.append(job.getJobID()==null?"":job.getJobID()).append(CRT001);
    	//是否爬取完成
    	sb.append(job.isFinished()!=null && job.isFinished()?"1":"0").append(CRT001);
    	//是否有请求 
    	sb.append(job.isRequested()!=null && job.isRequested()?"1":"0").append(CRT001);
    	//是否爬取成功 
//    	sb.append(job.isSuccessful()!=null && job.isSuccessful()?"1":"0").append(CRT001);
    	sb.append(successful).append(CRT001);
    	//爬取开始时间
    	sb.append(job.getStartTime()==null?"":DateUtil.formateDateStr(job.getStartTime())).append(CRT001);
    	//爬取结束时间
    	sb.append(job.getEndTime()==null?"":job.getEndTime()).append(CRT001);
    	//数据爬取业务系统
    	sb.append(job.getSystemID()==null?"":job.getSystemID()).append(CRT001);
    	//手机号码
    	sb.append(job.getQuery()==null?"":job.getQuery()).append(CRT001);
    	//重试次数
    	sb.append(job.getTryTimes()==null?"":job.getTryTimes()).append(CRT001);
    	//爬取结果类型 SUCCESSFUL
    	sb.append(result==null?"":result.getType()).append(CRT001);
//    	sb.append("SUCCESSFUL").append(CRT001);
    	//爬取结果消息
    	sb.append(result==null?"":result.getMessage()).append(CRT001);
    	//消息code
    	sb.append(result==null?"":result.getCode());
    	key.set("unionPaySmart");
    	val.set(sb.toString().replace("\n", " "));
    	context.write(key, val);
	}
	
	
}
